package ru.urfc.portal.ui.client.googlets.client;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.descriptor.CompositeGridField;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.renderers.ContractRenderer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

public class ContragentInfo extends BaseGooglet {

	interface Binder extends UiBinder<Widget, ContragentInfo> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
	@UiField Element name;
	@UiField Element judicialAddress;
	@UiField Element inn;
	@UiField Element kpp;
	@UiField Element ogrn;
	
	@UiField Element contracts;
	@UiField Element contractTemplate;
	@UiField Element contract;

	@UiField Element rfcRegistrationDate;
	@UiField Element postalAddress;
	@UiField Element head;
	@UiField Element bankName;
	@UiField Element bankAddress;
	@UiField Element bankOperatingAccount;
	@UiField Element bankCorrespontentAccount;
	@UiField Element bik;
	@UiField Element hozForm;
	@UiField Element phone;
	@UiField Element fax;
	@UiField Element email;
//	@UiField AnchorElement editAnchor;
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
//		editAnchor.setHref(PAGE_CONTRAGENT_EDIT.replace("{0}", getClientId()));
		
		GridDescriptor descriptor = createDescriptor(getClientId(),
				name, rfcRegistrationDate, postalAddress, head, phone, fax, email);

		descriptor.addCompositeField("judicialZip", "judicialCity", "judicialAddress").assign(judicialAddress, ", ");
		descriptor.addTextField("inn", inn);
		descriptor.addTextField("kpp", kpp);
		descriptor.addTextField("ogrn", ogrn);
		descriptor.addTextField("bankName", bankName);
		descriptor.addCompositeField("bankCity", "bankAddress").assign(bankAddress, ", ");
		descriptor.addTextField("bankOperatingAccount", bankOperatingAccount);
		descriptor.addTextField("bankCorrespontentAccount", bankCorrespontentAccount);
		descriptor.addTextField("bik", bik);
		descriptor.addTextField("hozForm", hozForm);
		createController(descriptor);

		createController(createDescriptor(getClientId(), contract), contracts, contractTemplate);
	}

	static public GridDescriptor createDescriptor(String clientId, Element contract) {
		GridDescriptor descriptor = new GridDescriptor(CONTRACT, CLIENTS, clientId, CONTRACTS);
		CompositeGridField compositeField = descriptor.addCompositeField();
		compositeField.addField(descriptor.addField("number"));
		compositeField.addField(descriptor.addDateField("issueDate"));
		compositeField.addField(descriptor.addDateField("expireDate"));
		descriptor.assign(new ContractRenderer(compositeField, contract));
		return descriptor;
	}
	
	static public GridDescriptor createDescriptor(String clientId,
			Element name, Element rfcRegistrationDate, Element postalAddress, 
			Element head, Element phone, Element fax, Element email) {
		GridDescriptor descriptor = new GridDescriptor(CONTRAGENT, CLIENTS, clientId, CLIENT_DETAILS);
		descriptor.addTextField("name", name);
		descriptor.addDateField("rfcRegistrationDate", rfcRegistrationDate);
		descriptor.addCompositeField("postalZip", "postalCity", "postalAddress").assign(postalAddress, ", ");
		descriptor.addTextField("head", head);
		descriptor.addTextField("phone", phone);
		descriptor.addTextField("fax", fax);
		descriptor.addTextField("email", email);
		return descriptor;
	}

}